<?php
define('IN_MOBILE', true);
require '../../../../framework/bootstrap.inc.php';
require IA_ROOT . '/addons/dashen_zaoqizhuanqian/extends/weixin.class.php';
global $_W, $_GPC;
$input = file_get_contents('php://input');
$obj = (Array) isimplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);

//查询订单编号数据是否存在
$order_info = pdo_fetch("SELECT * FROM " . tablename("zdaka_pay_record") . " WHERE `ordersn` = :ordersn" , array(':ordersn' => $obj['out_trade_no']));
if( !$order_info || $order_info['state'] == 1 ) {
	errors();
}
//获得系统配置
$setting = pdo_fetch('SELECT * FROM ' . tablename('zdaka_config') . ' WHERE uniacid ='.$order_info['uniacid']);
if( !$setting ) {
	errors();
}
$setting['settings'] = $setting['settings'] ? json_decode( $setting['settings'] , true ) : '';
$others =json_decode($setting['other_setting'],true);
$zhouqi1=!empty($others['zhouqi'])?intval($others['zhouqi']):21;
$zhouqi1 =  $zhouqi1>0?$zhouqi1-1:0;
$ysstate=!empty($set['ysstate'])?$set['ysstate']:0;
$bili =$setting['settings']['bili'];
$object = new weixin($setting['settings']);
$result = $object->notify( $obj );
//支付认证成功
if( $result['status'] == 'success' ) {
	$time = date('Y-m-d H:i:s',time());
	$today = date('Y-m-d',time());
	$huikuan=time()+3600*24*($zhouqi1);
	$huikuan_data = date('Y-m-d',$huikuan);
	$uniacid =  $order_info['uniacid']; //公众号id
	$uid = $order_info['user'];  //用户uid\
	$params['fee'] =  $order_info['fee'];  //金额
	
	pdo_update("zdaka_pay_record" , array("state" => 1) , array("id" => $order_info['id']));
	$gres= pdo_get('zdaka_pay_record', array("id" => $order_info['id'],'state' => 1));
	 if($gres['zstate']==1){
               $res= pdo_update('zdaka_yajin_record', array('yajin_money +=' => $params['fee'], 'bonus_num +=' => $params['fee']), array('openid' => $gres['openid'],'yajin_state'=>1));
             
                 exit();

            }
            $user = pdo_get('zdaka_user', array('openid' => $gres['openid']));

             if($ysstate==1){
                  $a=pdo_get('zdaka_yajin_record',array('openid'=>$gres['openid']));
                 if(!$a && $user['fuid']!=0){
                     if($user['fuid']!=0){
                         $fuser = pdo_update('zdaka_user', array('money +=' =>$params['fee']*$bili/100),array('id'=>$user['fuid']));
                         $data1=array(
                             'fuid'=>$user['fuid'],
                             'zuid'=>$user['id'],
                             'money'=>$params['fee']*$bili/100,
                             'date'=>$time,
                             'acid'=>$uniacid,
                         );
                         pdo_insert('zdaka_yqsy_record', $data1);
                         $data2 = array(
                             'uniacid' => $uniacid,
                             'money'     => $params['fee']*$bili/100,
                             'date' => $today,
                             'state' => 1,
                             'demo' => $today.'邀请支出'
                         );
                         pdo_insert('zdaka_yinli_record', $data2);
                     }
                 }
             }else{
                 if($user['fuid']!=0){
                     $fuser = pdo_update('zdaka_user', array('money +=' =>$params['fee']*$bili/100),array('id'=>$user['fuid']));
                     $data1=array(
                         'fuid'=>$user['fuid'],
                         'zuid'=>$user['id'],
                         'money'=>$params['fee']*$bili/100,
                         'date'=>$time,
                         'acid'=>$uniacid,
                     );
                     pdo_insert('zdaka_yqsy_record', $data1);
                     $data2 = array(
                         'uniacid' => $uniacid,
                         'money'     => $params['fee']*$bili/100,
                         'date' => $today,
                         'state' => 1,
                         'demo' => $today.'邀请支出'
                     );
                     pdo_insert('zdaka_yinli_record', $data2);
                 }
             }

            $data = array(
                'uniacid' => $uniacid,
                'uid' => $user['uid'],
                'openid' => $user['openid'],
                'headimgurl' => $user['headimgurl'],
                'nickname' => $user['nickname'],
                'yajin_money' => $params['fee'],
                'yajin_data' => $time,
                'apply_data' => $today,
                'yajin_state' => 1,
                'demo' => '支付挑战金成功',
                'is_daka' => 0,
                'huikuan_date' => $huikuan_data,
                'bonus_num' => $params['fee']
            );
            $rel = pdo_insert('zdaka_yajin_record', $data);
}
function errors() {
	exit("<xml><return_code><![CDATA[FAIL]]><return_code/><return_msg><![CDATA[ERROR]]><return_msg/></xml>");
}
function success() {
	exit("<xml><return_code><![CDATA[SUCCESS]]><return_code/><return_msg><![CDATA[OK]]><return_msg/></xml>");
}